package view.components
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.text.TextField;
	
	import global.Resource;
	
	import model.system.TraceManager;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	public class RemoteHtmlImg extends Sprite
	{
		private var dWidth:int;
		private var dHeight:int;
		private var dImg:Bitmap;
		private var imgText:TextField;
		public var isLimited:Boolean = true;
		
		public static var images:Array = [];
		public static function show(v:Boolean):void
		{
			for each (var o:RemoteHtmlImg in images)
			{
				o.showImg(v);
			}
		}
		
		public function RemoteHtmlImg(imgWidth:int,imgHeight:int,defaultImgKey:String="face_head")
		{
			this.mouseChildren = false;
			dWidth = imgWidth;
			dHeight = imgHeight;
			initDefaultImg(defaultImgKey);
			initText();
			images.push(this);
			
		}
		
		
		
		private function initText():void
		{
			imgText = TextTool.getTextField(dWidth + 5,dHeight + 5,12,0);
			imgText.x = - 2;
			imgText.y = - 2;
			imgText.wordWrap = true;
			imgText.multiline = true;
			addChild(imgText);
			
			
		}
		
		private function onIoError(event:IOErrorEvent):void
		{
			event.target.removeEventListener(IOErrorEvent.IO_ERROR,onIoError);
			clear();
		}
		
		private function initDefaultImg(defaultImgKey:String):void
		{
			dImg = Resource.getImg(defaultImgKey);
			dImg.width = dWidth;
			dImg.height = dHeight;
			addChild(dImg);
			
			if (dImg is TempBitmap)
			{
				dImg.addEventListener(Event.COMPLETE,onDImgComplete);
			}
		}
		
		private function onDImgComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onDImgComplete);
			dImg.width = dWidth;
			dImg.height = dHeight;
		}
		
		public function loadImg(url:String):void
		{
			clear();
			if (url== "")
			{
				return;
			}
			
			if (isLimited)
			{
				imgText.htmlText = "<img id='img' width='" + dWidth + "' height='" + dHeight + "' align='left' hspace='0' vspace='0' src='" + url + "'/>";
				trace(imgText.htmlText);
			}
			else
			{
				imgText.htmlText = "<img id='img' align='left' hspace='0' vspace='0' src='" + url + "'/>";
			}
			(imgText.getImageReference("img") as Loader).contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
			
			TraceManager.getInstance().out(url,dWidth,dHeight);
			DisplayTool.removeChild(this,dImg);
		}
		
		public function showImg(v:Boolean):void
		{
			if (v)
			{
				addChild(imgText);
			}
			else
			{
				
				DisplayTool.removeChild(imgText.parent,imgText);
			}
		}
		
		public function clear():void
		{
			if (imgText.getImageReference("img") != null)
			{
				(imgText.getImageReference("img") as Loader).contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onIoError);
			}
			
			imgText.htmlText = "";
			addChild(dImg);
		}
		
		public function destroy():void
		{
			clear();
			images.splice(images.indexOf(this),1);
			imgText = null;
			dImg.bitmapData = null;
			dImg = null;
			
			
		}
	}
}